﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class CloseDate: ICloseDate
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public CloseDate(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
        [Transaction(TransactionMode.Requires)]
        public DataTable GetCloseDateByDate(System.DateTime dt)
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = string.Format("SELECT * FROM CloseDate Where StartDate <= '{0}' AND EndDate >= '{1}'", dt.ToString("yyyy-MM-dd"), dt.ToString("yyyy-MM-dd"));

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
                //return Convert.ToDateTime(dtSearch.Rows[0][BEGDC.PreContract.DAL.CloseDate.CONST_Year].ToString() + "-" + dtSearch.Rows[0][BEGDC.PreContract.DAL.CloseDate.CONST_Month].ToString() + "-1");

            }
        }
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.CloseDate GetCloseDate(System.Int32 closeDateID)
		{
			BEGDC.PreContract.DAL.CloseDate closeDate = BEGDC.PreContract.DAL.CloseDate.TryFind(closeDateID);

            return closeDate;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetCloseDate_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM CloseDate";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteCloseDate(System.Int32 closeDateID)
		{
			try
            {
				BEGDC.PreContract.DAL.CloseDate closeDate = BEGDC.PreContract.DAL.CloseDate.TryFind(closeDateID);
                closeDate.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("CloseDate", string.Format("Error occured when deleting the CloseDate, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddCloseDate(System.Int32 year, System.Int32 month, System.Int32 quarter, Nullables.NullableDateTime startDate, Nullables.NullableDateTime endDate)	
		{
			try
            {
				BEGDC.PreContract.DAL.CloseDate closeDate = new BEGDC.PreContract.DAL.CloseDate();
				
				closeDate.Year = year;
				closeDate.Month = month;
				closeDate.Quarter = quarter;
				closeDate.StartDate = startDate;
				closeDate.EndDate = endDate;
				closeDate.CreateAndFlush();
				
				return closeDate.CloseDateID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("CloseDate", string.Format("Error occured when adding a new CloseDate, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateCloseDate(System.Int32 closeDateID, System.Int32 year, System.Int32 month, System.Int32 quarter, Nullables.NullableDateTime startDate, Nullables.NullableDateTime endDate)
		{
			try
            {
                BEGDC.PreContract.DAL.CloseDate closeDate = GetCloseDate(closeDateID);

				closeDate.Year = year;
				closeDate.Month = month;
				closeDate.Quarter = quarter;
				closeDate.StartDate = startDate;
				closeDate.EndDate = endDate;
				closeDate.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("CloseDate", string.Format("Error occured when updating the CloseDate, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



